﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_QuanTriNguoiDung : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_QuanTriNguoiDung()
        {
            InitializeComponent();
        }
        KetNoi_NguoiDung kn = new KetNoi_NguoiDung();
        public void LoadDuLieu()
        {

            gridControl1.DataSource = kn.Load_NguoiDung();
        }
        private void uc_QuanTriNguoiDung_Load(object sender, EventArgs e)
        {
            kn.connect();
            LoadDuLieu();
            btnHuy.Enabled = false;
            btnLuu.Enabled = false;
        }
        int flag;
        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 0;
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            kn.xoa_NguoiDung(txtMaNhanVien.Text);
            LoadDuLieu();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                kn.them_NguoiDung(txtTenDangNhap.Text, txtMaNhanVien.Text, txtMatKhau.Text, txtMaPhanQuyen.Text);

            }
            if (flag == 1)
            {
                kn.sua_NguoiDung(txtTenDangNhap.Text, txtMaNhanVien.Text, txtMatKhau.Text, txtMaPhanQuyen.Text);
            }
            LoadDuLieu();
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtTenDangNhap.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Tên Đăng Nhập").ToString();
            txtMaNhanVien.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mã Nhân Viên").ToString();
            txtMatKhau.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"Mật Khẩu").ToString();
            txtMaPhanQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Phân Quyền").ToString();
        }

        private void txtMaNhanVien_TextChanged(object sender, EventArgs e)
        {
            string s = "select * from tbl_NhanVien where MaNV='" + txtMaNhanVien.Text + "'";
            DataTable dt = new DataTable();
            dt = kn.taobang(s);
            if (dt.Rows.Count != 0)
                errorProvider1.Clear();
            else
            {
                errorProvider1.SetError(txtMaNhanVien, "Mã Nhân Viên Không Tồn Tại!");
                txtMaNhanVien.Focus();
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            gridControl1.ShowPrintPreview();
            
        }
    }
}
